package com.gzdh.dishrecognition.ui.adapter.report;

import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.gzdh.dishrecognition.R;

import java.math.BigDecimal;
import java.util.ArrayList;

public class OrderDetailsAdapter extends RecyclerView.Adapter<OrderDetailsAdapter.ViewHolder> {


    private JSONArray jsonArray;
    private Context mcontext;
    private BigDecimal price;
    private BigDecimal count;


    public OrderDetailsAdapter(Context context) {
        mcontext = context;
        this.jsonArray = new JSONArray();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_details, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (jsonArray.size() == position) {
            holder.amountLinear.setVisibility(View.VISIBLE);
            holder.foodsLinear.setVisibility(View.GONE);

            holder.countTv.setText("X" + count.toPlainString());
            holder.amountTv.setText("￥" + price.toPlainString());

        } else {
            holder.amountLinear.setVisibility(View.GONE);
            holder.foodsLinear.setVisibility(View.VISIBLE);

            holder.nameTv.setText(jsonArray.getJSONObject(position).getString("hname"));
            holder.slTv.setText("X" + jsonArray.getJSONObject(position).getString("sl"));
            holder.jeTv.setText("￥" + jsonArray.getJSONObject(position).getString("je"));

            count = count.add(new BigDecimal(jsonArray.getJSONObject(position).getString("sl")));
            price = price.add(new BigDecimal(jsonArray.getJSONObject(position).getString("je")));
        }
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;

        price = new BigDecimal(0);
        count = new BigDecimal(0);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return jsonArray.size() + 1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout foodsLinear;
        LinearLayout amountLinear;
        TextView nameTv;
        TextView slTv;
        TextView jeTv;
        TextView countTv;
        TextView amountTv;

        ViewHolder(View item) {
            super(item);
            foodsLinear = item.findViewById(R.id.foodsLinear);
            amountLinear = item.findViewById(R.id.amountLinear);
            nameTv = item.findViewById(R.id.nameTv);
            slTv = item.findViewById(R.id.slTv);
            jeTv = item.findViewById(R.id.jeTv);
            countTv = item.findViewById(R.id.countTv);
            amountTv = item.findViewById(R.id.amountTv);
        }

    }
}
